﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


// Khai báo thông tin mà bất cứ một nhân vật chính hay quái vật trong game đều có
namespace MyGames
{
    // Layer depth note:
    // Background layer: 0 - 0.1
    // Mainground layer: 0.3
    // Foreground layer: 0.9
    // Player, enemies and project: 0.8 - 0.85 (base on Y value)
    // Player skill: 0.9
    // Particles: 0.95
    // Items: base on Y value: 0.8 - 0.85

    class Character : GameObject
    {
        #region Movement
        protected float nextActionTime;
        protected float invicibleEndTime;
        protected bool gethit;
        protected bool invicible;
        #endregion

        #region Declaration
        protected float maxHealth;
        protected float maxMana;
        protected float health;
        protected float mana;
        protected int attackPower;
        protected int armor; // áo giáp
        protected float speed;
        protected float attackSpeed;
        protected float healthRegen;
        protected float manaRegen;
        #endregion

        #region Properties
        public float MaxHealth
        {
            get { return maxHealth; }
            set
            {
                float p = health / maxHealth;
                maxHealth = value;
                health = p * maxHealth;
            }
        }

        public float MaxMana
        {
            get { return maxMana; }
            set
            {
                float p = mana / maxMana;
                maxMana = value;
                mana = p * maxMana;
            }
        }

        public float Health
        {
            get { return health; }
            set { health = value; }
        }

        public float Mana
        {
            get { return mana; }
            set { mana = value; }
        }

        public int AttackPower
        {
            get { return attackPower; }
            set { attackPower = value; }
        }

        public int Armor
        {
            get { return armor; }
            set { armor = value; }
        }

        public float Speed
        {
            get { return speed; }
            set { speed = value; }
        }

        public float AttackSpeed
        {
            get { return attackSpeed; }
            set { attackSpeed = value; }
        }

        public float HealthRegen
        {
            get { return healthRegen; }
            set { healthRegen = value; }
        }

        public float ManaRegen
        {
            get { return manaRegen; }
            set { manaRegen = value; }
        }

        public bool Invicible
        {
            get { return invicible; }
        }
        #endregion

        #region Update and Draw
        public override void Update(GameTime gameTime)
        {
            // Redefine layer
            drawDepth = 0.8f + worldLocation.Y / 10000;

            // Regen health and mana
            float time = (float)gameTime.ElapsedGameTime.TotalSeconds;
            health = MathHelper.Clamp(health + time * maxHealth * healthRegen, 0f, maxHealth);
            mana = MathHelper.Clamp(mana + time * maxMana * manaRegen, 0f, maxMana);

            // Dead, stop regening
            if (health == 0) healthRegen = 0f;

            base.Update(gameTime);
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            // Draw healthbar of the character when he's on screen
            if (Camera.ObjectIsVisible(WorldRectangle))
            {
                int h = animations[currentAnimation].FrameHeight;
                Vector2 screen = Camera.WorldToScreen(worldLocation);
                Vector2 pos = new Vector2(screen.X - 40, screen.Y - h - 10);

                // Draw the container
                spriteBatch.Draw(GUI.BarGUI.HealthBar.container, pos, null, Color.White,
                    0f, Vector2.Zero, 1f, SpriteEffects.None, drawDepth - 0.001f);

                // Draw health and mana
                pos.X += 4; pos.Y += 1;
                spriteBatch.Draw(GUI.BarGUI.HealthBar.health, pos, null, Color.White, 0f, Vector2.Zero,
                    new Vector2(health / maxHealth, 1f), SpriteEffects.None, drawDepth);
                pos.Y += 3;
                spriteBatch.Draw(GUI.BarGUI.HealthBar.mana, pos, null, Color.White, 0f, Vector2.Zero,
                    new Vector2(mana / maxMana, 1f), SpriteEffects.None, drawDepth);
            }

            base.Draw(spriteBatch);
        }
        #endregion
    }
}
